package listeners

import (
	"blockchains-aio/db"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func RegisterSlogan(router *gin.Engine) {
	router.POST("/slogan", addSlogan)
	router.GET("/slogan", getSlogan)
}

func addSlogan(ctx *gin.Context) {
	name := ctx.PostForm("name")
	url := ctx.PostForm("url")

	if len(name) == 0 || len(url) == 0 {
		ctx.JSON(400, fmt.Sprintf("Missing parameter name or url"))
		return
	}

	db.CreateSlogan(name, "", url)
	ctx.JSON(http.StatusOK, "")
}

func getSlogan(ctx *gin.Context) {
	slogan := db.GetSlogan()

	if slogan == nil {
		ctx.JSON(400, fmt.Sprintf("System not initialized"))
		return
	}

	ctx.JSON(http.StatusOK, *slogan)
}
